package com.gjh.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.gjh.controller.BeanController;
import com.gjh.domain.cargo.ContractProduct;
import com.gjh.domain.cargo.ContractProductExample;
import com.gjh.domain.cargo.Factory;
import com.gjh.domain.cargo.FactoryExample;
import com.gjh.service.cargo.ContractProductService;
import com.gjh.service.cargo.FactoryService;
import com.gjh.utils.DownloadUtil;
import com.gjh.utils.FileUploadUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/contractProduct")
public class ContractProductController extends BeanController {
    @Reference
    private FactoryService factoryService;
    @Reference
    private ContractProductService contractProductService;
    @Autowired
    private FileUploadUtil fileUploadUtil;


    @RequestMapping(value="/list",name="展示合同下货物列表数据")
    public String list(String contractId, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int size){
        ContractProductExample contractProductExample = new ContractProductExample();
        ContractProductExample.Criteria criteria = contractProductExample.createCriteria();
        criteria.andContractIdEqualTo(contractId);
        PageInfo pageInfo = contractProductService.findAll(contractProductExample, page, size,contractId);
        request.setAttribute("page",pageInfo);

        //查询所有工厂
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria1 = factoryExample.createCriteria();
        criteria1.andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        request.setAttribute("contractId",contractId);
        return "/cargo/product/product-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "cargo/contract/contract-add";
    }

    @RequestMapping(value = "/toUpdate",name = "进入修改合同下货物页面")
    public String toUpdate(String id){
        //获取当前货物对象
        ContractProduct product = contractProductService.findById(id);
        request.setAttribute("contractProduct",product);
        //获取所有工厂
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        criteria.andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);
        return "/cargo/product/product-update";
    }

    @RequestMapping(value = "/edit",name = "保存货物方法")
    public String edit(ContractProduct contractProduct,MultipartFile productPhoto){
        try {
            String upload = fileUploadUtil.upload(productPhoto);
            contractProduct.setProductImage(upload);//把七牛云返回的地址添加到表中
        } catch (Exception e) {
            e.printStackTrace();
        }
        //获取是否有id
        if(StringUtils.isEmpty(contractProduct.getId())){
            //新增
            contractProduct.setId(UUID.randomUUID().toString());
            contractProduct.setCreateTime(new Date());
            contractProductService.save(contractProduct);
        }else {
            //修改
            contractProduct.setUpdateTime(new Date());
            contractProductService.update(contractProduct);
        }
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractProduct.getContractId();
    }

    @RequestMapping(value = "/delete",name = "删除合同下的货物")
    public String delete (String id,String contractId){
        contractProductService.delete(id);
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }

    @RequestMapping(value = "/toImport",name = "进入给合同上传货物的页面")
    public String toImport(String contractId){
        request.setAttribute("contractId",contractId);
        return "cargo/product/product-import" ; //重定向到列表页面
    }

    @RequestMapping(value = "/import",name = "合同上传货物的方法")
    public String importXls(String contractId,MultipartFile file) throws IOException {
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
        int lastRowIndex = sheet.getLastRowNum();

        List<ContractProduct> productList = new ArrayList<>();
        ContractProduct contractProduct=null;
        Row row=null;
        for (int i = 1; i < lastRowIndex; i++) {
             contractProduct = new ContractProduct();
             row=sheet.getRow(i);
             //读取每一行的内容
            String factorName = row.getCell(1).getStringCellValue();
            contractProduct.setFactoryName(factorName);//生产厂家

            String productNo = row.getCell(2).getStringCellValue();
            contractProduct.setProductNo(productNo);//货号

            Double cnumber = row.getCell(3).getNumericCellValue(); //是数值型，取到的数值型其实是Double
            contractProduct.setCnumber(cnumber.intValue());//数量

            String packingUnit = row.getCell(4).getStringCellValue();
            contractProduct.setPackingUnit(packingUnit);// 包装单位(PCS/SETS)

            Double loadingRate = row.getCell(5).getNumericCellValue();
            contractProduct.setLoadingRate(loadingRate.toString());//装率

            Double boxNum = row.getCell(6).getNumericCellValue();
            contractProduct.setBoxNum(boxNum.intValue());//箱数

            Double price  = row.getCell(7).getNumericCellValue();
            contractProduct.setPrice(price);//单价

            String productDesc = row.getCell(8).getStringCellValue();
            contractProduct.setProductDesc(productDesc);//货物描述

            String productRequest = row.getCell(9).getStringCellValue();
            contractProduct.setProductRequest(productRequest);//要求

            contractProduct.setId(UUID.randomUUID().toString());
            contractProduct.setContractId(contractId);
            contractProduct.setCreateTime(new Date()); //创建时间
            productList.add(contractProduct);
        }
        contractProductService.saveList(productList);
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId ;
    }


}
